package com.example.zzm.yuejian03.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.zzm.yuejian03.Activity.FabuActivity;
import com.example.zzm.yuejian03.Adapter.PictureAdapter;
import com.example.zzm.yuejian03.Bean.PictureBean;
import com.example.zzm.yuejian03.Listener.PictureListener;
import com.example.zzm.yuejian03.Model.PictureModel;
import com.example.zzm.yuejian03.R;

import java.util.List;

public class PictureFragment extends BasesFragment{
    private List<PictureBean> list;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager  layoutManager;
    private PictureAdapter adapter;
    private View view;
    private ImageView imageView10;

    private PictureListener listener = new PictureListener() {

        @Override
        public void onResponse(List<PictureBean> beanlist) {
            list=beanlist;
            adapter.setList(list);
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context, "失败："+msg, Toast.LENGTH_SHORT).show();

        }
    };

    public PictureFragment() {    }
    @Nullable
    @Override //生命周期方法，创建View
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_picture,container,false);
        initView();
        initEvent();
        return view;
    }

    void initView(){
        imageView10 = (ImageView) view.findViewById(R.id.imageView10);
    }
    void initEvent(){
        imageView10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), FabuActivity.class));
            }
        });
    }
    @Override//生命周期方法，View创建完成
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //System.out.println("--f1--"+getSessionId());
        initRecyclerView(view);
        PictureModel model=new PictureModel();
        model.getPictureList(listener);
    }
    private void initRecyclerView(View view) {
        recyclerView=view.findViewById(R.id.recyclerview);
        layoutManager=new LinearLayoutManager(context);
        GridLayoutManager gridLayoutManager =new GridLayoutManager(getContext(),2);

        recyclerView.setLayoutManager(gridLayoutManager);
        //每个item如果是确定高度，设置此项提高性能
        recyclerView.setHasFixedSize(true);
        //实例化适配器
        adapter=new PictureAdapter(context);
        recyclerView.setAdapter(adapter);

    }


    public interface OnFragmentInteractionListener {
    }
}
